<template>
    <div class="contentmain">
        <el-row>
            <el-col :span="5" style="float:left">
                <el-button v-if="!shiro" type="primary" icon="el-icon-plus" @click="newadd">添加盘点单</el-button>
                <el-button type="primary" @click="out">导出</el-button>
            </el-col>
            <el-col :span="14" style="float:right">
                <el-form :inline="true" :model="formInline" class="demo-form-inline">
                    <el-form-item style="float:right">
                        <el-button type="primary" @click="onSubmit">查询</el-button>
                    </el-form-item>
                    <el-form-item style="float:right">
                        <el-date-picker
                            v-model="formInline.value4"
                            type="datetimerange"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item style="float:right">
                        <el-input v-model="formInline.goodid" placeholder="商品编号"></el-input>
                    </el-form-item>
                    <el-form-item style="float:right">
                        <el-input v-model="formInline.goodname" placeholder="商品名称"></el-input>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="24">
                <el-table ref="multipleTable" :data="td" tooltip-effect="dark" style="width: 100%" v-loading="loading" element-loading-text="数据加载中...">
                    <el-table-column label="门店">
                        <template slot-scope="scope">
                            {{shopname(scope.row.shopId)}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="goodId" label="商品编号">
                    </el-table-column>
                    <el-table-column prop="goodName" label="商品名称">
                    </el-table-column>
                    <el-table-column prop="spec" label="单位">
                    </el-table-column>
                    <el-table-column prop="sysStock" label="系统库存">
                    </el-table-column>
                    <el-table-column prop="realStock" label="实际库存">
                    </el-table-column>
                    <el-table-column prop="difStock" label="差异库存">
                    </el-table-column>
                    <el-table-column label="盘点时间">
                        <template slot-scope="scope">
                            {{scope.row.checkTime | format}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="remark" label="备注">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="detial(scope.$index, scope.row)">查看更多</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>

        <CreateInventory />
        <el-row>
            <el-col :span="8" :offset="16">
                <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-size="10"
                layout="total, prev, pager, next, jumper"
                :total="pagecount">
                </el-pagination>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import moment from 'moment'
import CreateInventory from 'components/Drp/CreateInventory'
export default{
    data(){
        return{
            formInline:{},
            td:[],
            pagecount:0,
            currentPage:1,
            loading:false,
            opt:[]
        }
    },
    filters:{
        format(v){
            return moment(v).format("YYYY-MM-DD")
        }
    },
    components:{
        CreateInventory
    },
    computed:{
        getstate(){
            return this.$store.state.TOPshopID;
        },
        getdialog(){
            return this.$store.state.createpandian;
        },
        shiro(){
            return this.$store.state.shiro
        }
    },
    watch:{
        getstate(v){//获取右上角的指定店铺
            if(v){
                var pp=new URLSearchParams()
                if(!this.$store.state.shiro){
                    return
                }
                if(this.formInline.goodname){
                    pp.append('goodName',this.formInline.goodname)
                }
                if(this.formInline.goodid){
                    pp.append('goodId',this.formInline.goodid)
                }
                if(this.formInline.value4){
                    pp.append('checkStartTime',moment(this.formInline.value4[0]).format("YYYY-MM-DD HH:mm:ss"))
                    pp.append('checkEndTime',moment(this.formInline.value4[1]).format("YYYY-MM-DD HH:mm:ss"))
                }
                if(this.$store.state.TOPshopID!=-1){
                    pp.append('shopId',this.$store.state.TOPshopID) 
                }
                this.getData(pp)
            }
        },
        getdialog(v){//获取弹窗信息
            if(!v){
                this.$store.state.createpandian=true
                if(this.$store.state.shiro){
                    this.getData()
                }else{
                    var pp=new URLSearchParams()
                    pp.append('shopId',this.$store.state.TOPshopID)
                    this.getData(pp)
                }
            }
        }
    },
    created(){
        var _this=this
        this.opt=[]
        axios.get('/shop/listShop.do').then(r=>{
            if(r.data.code!=200){
                _this.$message.error(r.data.message)
                return
            }
            _this.opt=[].concat(r.data.response.shopList)
        })
    },
    mounted(){
        if(isDetailBack){
            var pp=new URLSearchParams()
            if(!this.$store.state.shiro){
                pp.append('shopId',this.$store.state.TOPshopID)
            }
            this.getData(pp)
        }
    },
    methods:{
        shopname(v){//将shopId转为店铺名称
            for(var i=0;i<this.opt.length;i++){
                if(v==this.opt[i].shopId){
                    return this.opt[i].shopName
                }
            }
        },
        onSubmit(){//搜索
            var pp=new URLSearchParams()
            if(this.formInline.goodname){
                pp.append('goodName',this.formInline.goodname)
            }
            if(this.formInline.goodid){
                pp.append('goodId',this.formInline.goodid)
            }
            if(this.formInline.value4){
                pp.append('checkStartTime',moment(this.formInline.value4[0]).format("YYYY-MM-DD HH:mm:ss"))
                pp.append('checkEndTime',moment(this.formInline.value4[1]).format("YYYY-MM-DD HH:mm:ss"))
            }
            if(this.$store.state.TOPshopID && this.$store.state.TOPshopID!=-1){
                pp.append('shopId',this.$store.state.TOPshopID)
            }
            this.getData(pp)
        },
        detial(i,r){//跳转详情页
            this.$router.push({path: '/Detial', query: {id: r.goodId,type:3}})
        },
        handleSizeChange(i){//翻页
            this.topage(i)
        },
        handleCurrentChange(i){//翻页
            this.topage(i)
        },
        topage(i){//翻页
            var pp=new URLSearchParams()
            pp.append('pageNo',i)
            if(this.formInline.goodname){
                pp.append('goodName',this.formInline.goodname)
            }
            if(this.formInline.goodid){
                pp.append('goodId',this.formInline.goodid)
            }
            if(this.formInline.value4){
                pp.append('checkStartTime',moment(this.formInline.value4[0]).format("YYYY-MM-DD HH:mm:ss"))
                pp.append('checkEndTime',moment(this.formInline.value4[1]).format("YYYY-MM-DD HH:mm:ss"))
            }
            if(this.$store.state.TOPshopID && this.$store.state.TOPshopID!=-1){
                pp.append('shopId',this.$store.state.TOPshopID)
            }
            this.getData(pp)
        },
        newadd(){//新增
            this.$store.commit('ChangeCreateInventory')
        },
        out(){//导出
            var _this=this
            var pp=new URLSearchParams()
            if(this.formInline.goodname){
                pp.append('goodName',this.formInline.goodname)
            }
            if(this.formInline.goodid){
                pp.append('goodId',this.formInline.goodid)
            }
            if(this.formInline.value4){
                pp.append('checkStartTime',moment(this.formInline.value4[0]).format("YYYY-MM-DD HH:mm:ss"))
                pp.append('checkEndTime',moment(this.formInline.value4[1]).format("YYYY-MM-DD HH:mm:ss"))
            }
            if(this.$store.state.TOPshopID && this.$store.state.TOPshopID!=-1){
                pp.append('shopId',this.$store.state.TOPshopID)
            }
            window.open(HOST+'/getCheckByExcel.do?'+pp.toString())
            
        },
        getData(v){//获取数据
            var _this=this
            this.loading=true
            this.td=[]
            axios.get('/findCheckList.do',{params:v}).then(r=>{
                if(r.data.code==200){
                    _this.td=[].concat(r.data.response.goodCheckList)
                    _this.pagecount=r.data.response.page.totalCount
                    _this.loading=false
                }else{
                    if(r.data.message=="not login"&&r.data.code==500){
                        _this.$message.error("访问者,请登录")
                        router.push('/Login');
                        return
                    }
                    _this.$message.error(r.data.message)
                    _this.loading=false
                }
            })
        }
    }
}
</script>
<style>

</style>
